package main

/**
定时器 ticker
其实就是包含timer类型的channel
 */

 import (
	"fmt"
	"time"
)

func main() {
	//Tickers use a similar mechanism to timers: a channel that is sent values.
	// Here we’ll use the range builtin on the channel to iterate over the values as they arrive every 1s.

	ticker := time.NewTicker(1 * time.Second)
	go func() {
		for t := range ticker.C {
			fmt.Println("Tick at", t)
		}
	}()
	//Tickers can be stopped like timers.
	// Once a ticker is stopped it won’t receive any more values on its channel. We’ll stop ours after 3s.

	time.Sleep(3 * time.Second)
	ticker.Stop()
	fmt.Println("Ticker stopped")
}
